********************************************************************************
** 	TITLE: e15_predict_risk_het.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Figure XV: Heterogeneity by Predicted Recidivism Risk

********************************************************************************

******************************************
* Load Data
******************************************
set sortseed 13

	use "$output_data/prediction.dta",clear

******************************************
* Make Figure
******************************************

	* Heterogeneity by quartiles
		cap drop risk_tile*
		xtile risk_tile = return_3m_hat if booking_year>=2016 , n(4)


		forvalues t=2/4{
			gen risk_tile_`t' = risk_tile==`t'
			}
		
		//To preserve size restriction of variable names 	
			rename num_maj_weekly n_maj_week

		foreach v in  n_maj_week return_3m {
					//stay ignite_0 for misconduct
			if "`v'" == "n_maj_week"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
					//stay ignite_0 for Recidivism
			if "`v'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}

***** interaction terms			
		cap drop *_grpvar*

		forvalues t=2/4{
			foreach c of varlist ignite_0 stay D_ignite_proxy interact_proxy_any D_remove_any {
										
										gen `c'_grpvar_`t' = `c' * risk_tile_`t'
										
										}
									}

***** IV Regression	
		ivreghdfe `v'  ( ignite_0_grpvar_* stay_grpvar_* ignite_0 stay = ///
						interact_proxy_any_grpvar_* D_remove_any_grpvar_* interact_proxy_any D_remove_any) ///
						D_ignite_proxy_grpvar_* D_ignite_proxy risk_tile_* $auxiliary_control ///
						if `v'>=0&booking_year>2015,  ///
						cluster(inmate) ///
						a($design_control ) 
						
//IGNITE Effect by Quartiles
			mat R=J(10,4,.)
			forvalues t=1/4{
				if `t'==1{
					lincom ignite_0
				}
				else{
					lincom ignite_0_grpvar_`t'+ignite_0
				}
				
				local c=r(estimate)
				local ul=r(ub)
				local ll=r(lb)
				mat R[`t',1] = `c'
				mat R[`t',2] = `ul'
				mat R[`t',3] = `ll'
				mat R[`t',4] = `t'
			}
			
//Jail Effect by Quartiles
			mat M=J(10,4,.)
			forvalues t=1/4{
				if `t'==1{
					lincom stay
				}
				else{
					lincom stay_grpvar_`t' + stay
				}
				
				local c=r(estimate)
				local ul=r(ub)
				local ll=r(lb)
				mat M[`t',1] = `c'
				mat M[`t',2] = `ul'
				mat M[`t',3] = `ll'
				mat M[`t',4] = `t'
			}				
			local xt "Quartiles of predicted recidivism risk"

/*********************************************************************************
Panel (a) Effect of Months in IGNITE on Misconduct
Panel (b) Effect of Months in IGNITE on Recidivism
**********************************************************************************/				
			preserve		

			svmat R
			keep if !mi(R1)
			keep R1-R4
			ren (R1 R2 R3 R4) (c ul ll t)
			local xub = 4+.2
			tw (rcap ul ll t, horizontal lc(black) mc(black)) ///
				(scatter t c, lc(black) mc(black)), ///
				xlab(-.6(.3).3) xline(0, lc(black)) ysc(r(.8 `xub')) ylab(1 "(Low) 1" 2 "2" 3 "3" 4 "(High) 4")  ///
				legend(off) ytitle("`xt'") xtitle(" ") ///
				aspect(1)
				
			gr export "$figures/`v'_by_predrisk_4.pdf", replace
			restore

/*********************************************************************************
Panel (c) Effect of Months in Jail on Misconduct
Panel (d) Effect of Months in Jail on Recidivism
**********************************************************************************/
			preserve
			
			svmat M
			keep if !mi(M1)
			keep M1-M4
			ren (M1 M2 M3 M4) (c ul ll t)
			local xub = 4+.2
			tw (rcap ul ll t, horizontal lc(black) mc(black)) ///
				(scatter t c, lc(black) mc(black)), ///
				xlab(-.6(.3).3) xline(0, lc(black)) ysc(r(.8 `xub')) ylab(1 "(Low) 1" 2 "2" 3 "3" 4 "(High) 4")  ///
				legend(off) ytitle("`xt'") xtitle(" ") ///
				aspect(1)
				
			gr export "$figures/`v'_by_predrisk_4_time_in_jail.pdf", replace
			restore
			}
